package cn.itcast.service;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParser;

import android.util.Log;
import android.util.Xml;

import cn.itcast.domain.Student;
import cn.itcast.utils.MyContentHandler;
import cn.itcast.utils.StreamTool;

public class VideoService {
	
	public static List<Student> getLastStudent() throws Exception{
		String path = "http://192.168.2.212:8080/videoweb/video/list.do";
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setReadTimeout(5*1000);
		conn.setRequestMethod("GET");
		InputStream inStream = conn.getInputStream();
		return parseXML(inStream);
	}
	
	public static List<Student> getJSONLastVideos() throws Exception{
		List<Student> videos = new ArrayList<Student>();
		//要是也是本机系统的服务一定不能使用localhost
		String path = "http://192.168.2.212:8080/videoweb/video/list.do?format=json";
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setReadTimeout(5*1000);
		conn.setRequestMethod("GET");
		InputStream inStream = conn.getInputStream();
		byte[] data = StreamTool.readInputStream(inStream);
		String json = new String(data);
		JSONArray array = new JSONArray(json);
		for(int i=0 ; i < array.length() ; i++){
			JSONObject item = array.getJSONObject(i);
			int id = item.getInt("id");
			String name = item.getString("name");
			int age = item.getInt("age");
			videos.add(new Student(id, name, age));
		}
		return videos;
	}
	
	
	private static List<Student> parseSaxXML(InputStream inStream){
		List<Student> students = null;
		try {
			String resultStr = StreamTool.readInputStringStream(inStream);
			System.out.println(resultStr);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try{
			//创建一个SAXParserFactory
			SAXParserFactory factory = SAXParserFactory.newInstance();
			XMLReader reader = factory.newSAXParser().getXMLReader();
			//为XMLReader设置内容处理器
			reader.setContentHandler(new MyContentHandler());
			//开始解析文件
			reader.parse(new InputSource(inStream));
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return students;
	}
	/**
	 * @param inStream
	 * @return
	 * @throws Exception
	 */
	private static List<Student> parseXML(InputStream inStream) throws Exception{
		List<Student> students = null;
		Student student = null;
//		StreamTool.readInputStringStream(inStream);
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inStream, "UTF-8");
		int eventType = parser.getEventType();
		while(eventType!=XmlPullParser.END_DOCUMENT){
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				students = new ArrayList<Student>();
				break;
			case XmlPullParser.START_TAG:
				String name = parser.getName();
				if("video".equals(name)){
					student = new Student();
					student.setId(new Integer(parser.getAttributeValue(0)));
				}
				if(student!=null){
					if("name".equals(name)){
						student.setName(parser.nextText());
					}
					if("age".equals(name)){
						student.setAge(new Integer(parser.nextText()));
					}
				}
				break;
				
			case XmlPullParser.END_TAG:
				if("name".equals(parser.getName())){
					students.add(student);
					student = null;
				}
				break;
			}
			eventType = parser.next();
		}
		return students;
	}
}
